package org.springblade.modules.repair.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.cache.DictCache;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.flow.business.service.FlowBusinessService;
import org.springblade.flow.core.constant.ProcessConstant;
import org.springblade.flow.core.entity.BladeFlow;
import org.springblade.modules.repair.entity.BusinessWork;
import org.springblade.modules.repair.enums.ApproveStatusEnum;
import org.springblade.modules.repair.enums.EmerProcTypeEnum;
import org.springblade.modules.repair.enums.MaintainPlanContentEnum;
import org.springblade.modules.repair.enums.TaskStatusEnum;
import org.springblade.modules.repair.service.BusinessWorkService;
import org.springblade.modules.repair.vo.BusinessApprovePageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.repair.mapper.BusinessApproveMapper;
import org.springblade.modules.repair.entity.BusinessApprove;
import org.springblade.modules.repair.service.BusinessApproveService;

@Service
public class BusinessApproveServiceImpl extends BaseServiceImpl<BusinessApproveMapper, BusinessApprove> implements BusinessApproveService {

	@Autowired
	private BusinessWorkService businessWorkService;
	@Autowired
	private FlowBusinessService flowBusinessService;

	@Override
	public IPage<BusinessApprovePageVO> selectPage(BusinessApprove businessApprove, IPage<BusinessApprovePageVO> page) {
		return page.setRecords(baseMapper.page(businessApprove,page));
	}

	@Override
	public HashMap<String, Object> getApproveBuildingDetail(Long businessApproveId) {
		BusinessApprove businessApprove = this.getById(businessApproveId);
		BusinessWork businessWork = businessWorkService.getById(businessApprove.getBusinessWorkId());
		HashMap<String,Object> resultMap = new HashMap<>(3);
		resultMap.put("businessApproveId",businessApprove.getId());
		resultMap.put("approveStatus",businessApprove.getApproveStatus());
		resultMap.put("buildingSubmitVO",businessWork.getUpdateData());
		return resultMap;
	}

	@Override
	public HashMap<String, Integer> getUserApproveCount(Long userId) {
		int pendingCount = this.count(Wrappers.<BusinessApprove>lambdaQuery().like(BusinessApprove :: getApproveIds, userId).eq(BusinessApprove :: getApproveStatus, ApproveStatusEnum.PENDING.getId()));
		int passedCount = this.count(Wrappers.<BusinessApprove>lambdaQuery().eq(BusinessApprove :: getApproveIds, userId).in(BusinessApprove :: getApproveStatus, ApproveStatusEnum.PASSED.getId(), ApproveStatusEnum.REJECT.getId()));
		BladeFlow bladeFlow = new BladeFlow();
		bladeFlow.setProcessDefinitionKey(ProcessConstant.PARTS);
		List<BladeFlow> todoList = flowBusinessService.selectTodoList(bladeFlow);
		List<BladeFlow> doneList = flowBusinessService.selectDoneList(bladeFlow);
		HashMap<String,Integer> resultMap = new HashMap<>(2);
		resultMap.put("pendingCount",pendingCount+todoList.size());
		resultMap.put("passedCount",passedCount+doneList.size());
		return resultMap;
	}

	@Override
	public IPage<Map<String, Object>> malfunctionPage(Map<String, Object> param, IPage<Map<String, Object>> page) {
		Long curUserId = AuthUtil.getUserId();
		param.put("userId", curUserId);
		List<Map<String, Object>> list = baseMapper.malfunctionPage(param, page);
		for (Map<String, Object> map : list) {
			if (Func.isNotEmpty(map.get("problemItem"))) {
				String problemItem = String.valueOf(map.get("problemItem"));
				List<String> problemNames = new ArrayList<>();
				List<String> problemItemKey = Func.toStrList(problemItem);
				for (String key : problemItemKey) {
					String problemName = DictCache.getValue("fault_problem", key);
					problemNames.add(problemName);
				}
				map.put("problemNames", problemNames);
			}

			if (Func.isNotEmpty(map.get("partsNames"))) {
				map.put("partsNames", String.valueOf(map.get("partsNames")).replace("&amp;&amp;",">"));
			}

			map.put("emerProcTypeName", EmerProcTypeEnum.getName(Integer.parseInt(String.valueOf(map.get("emerProcType")))));
			map.put("workOrderTypeName", EmerProcTypeEnum.getName(Integer.parseInt(String.valueOf(map.get("workOrderType")))));

			// 工单状态名称
			if (Func.isNotEmpty(map.get("status"))) {
				Integer status = Integer.valueOf(String.valueOf(map.get("status")));
				map.put("statusName", TaskStatusEnum.getTaskName(MaintainPlanContentEnum.MAINTAIN_WX.getId(),status));
			}
		}
		return page.setRecords(list);
	}

	@Override
	public BusinessApprove getByWorkOrderId(Long workOrderId) {
		return this.getOne(Wrappers.<BusinessApprove>lambdaQuery().eq(BusinessApprove :: getWorkOrderId, workOrderId), false);
	}
}

